<?php

namespace App\Http\Controllers;

use App\Book;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class BookController extends Controller
{
    // 保存
    public function save(Request $request) {
        $data = $request->all();
        $book = Book::query()->find($request->get('id'));
        if ($book) {
            $book->update($data);
            return response()->json(['code' => '200', 'msg' => '更新成功']);
        }
        Book::query()->create($data);
        return response()->json(['code' => '200', 'msg' => '插入成功']);
    }

    // 删除（根据ID）
    public function delete(Request $request) {
        $id = $request->get('id');
        Book::destroy($id);
        return response()->json(['code' => '200', 'msg' => '删除成功']);
    }

    // 查询全部
    public function list(Request $request) {
        $pageSize = $request->get('pageSize') ? $request->get('pageSize') : 2;
        $res = Book::query()->paginate($pageSize);
        return response()->json(['code' => '200', 'msg' => '获取成功', 'data' => $res]);
    }

    // 查询单个（根据ID）
    public function one(Request $request) {
        $res = Book::query()->find($request->get('id'));
        return response()->json(['code' => '200', 'msg' => '获取成功', 'data' => $res]);
    }

    // 查询热门推荐的图书
    public function hot() {
        $bookIds = DB::table('book_hot')->select('bookId')->get();
        $books = array();
        foreach ($bookIds as $item) {
            $book = Book::query()->find($item->bookId);
            array_push($books, $book);
        }
        return response()->json(['code' => '200', 'msg' => '获取成功', 'data' => $books]);
    }

    // 查询新书
    public function new() {
        $bookIds = DB::table('book_new')->select('bookId')->get();
        $books = array();
        foreach ($bookIds as $item) {
            $book = Book::query()->find($item->bookId);
            array_push($books, $book);
        }
        return response()->json(['code' => '200', 'msg' => '获取成功', 'data' => $books]);
    }

    // 搜索图书
    public function search(Request $request) {
        $wd = $request->get('wd');
        $pageSize = $request->get('pageSize') ? $request->get('pageSize') : 2;
        $res = Book::query()->where('title', 'like', "%$wd%")->paginate($pageSize);
        return response()->json(['code' => '200', 'msg' => '获取成功', 'data' => $res]);
    }

    // 查询图书（根据分类ID）
    public function category(Request $request) {
        $categoryId = $request->get('categoryId');
        $pageSize = $request->get('pageSize') ? $request->get('pageSize') : 2;
        $bookIds = DB::table('category_book')->where('categoryId', $categoryId)->pluck('id')->toArray();
        $res = Book::query()->whereIn('id', $bookIds)->paginate($pageSize);
        return response()->json(['code' => '200', 'msg' => '获取成功', 'data' => $res]);
    }

    // 生成图片地址（/img/图书id.jpg)
    public function test() {
        $books = Book::query()->get();
        foreach ($books as $item) {
            $b = Book::query()->find($item->id);
            $b->imgUrl = '/img/' . $item->id . '.jpg';
            $b->save();
        }
    }
}
